package com.syf.core.actuator;

import com.syf.core.service.SyService;
import com.syf.core.service.SyServiceDefinition;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 发布SyServiceDefinition信息
 */
public class SyServiceDefinitionPublisher {

    private Map<String, SyServiceDefinition> byCodeMap = new HashMap<>();

    private Map<Class<? extends SyService>, SyServiceDefinition> byClassMap = new HashMap<>();

    private static final SyServiceDefinitionPublisher INSTANCE = new SyServiceDefinitionPublisher();

    private SyServiceDefinitionPublisher() {
    }

    public static SyServiceDefinitionPublisher getInstance() {
        return INSTANCE;
    }

    void initializer(Collection<SyServiceDefinition> definitions) {
        for (SyServiceDefinition definition : definitions) {
            byCodeMap.put(definition.getServiceCode(), definition);
            byClassMap.put(definition.getServiceClass(), definition);
        }
    }

    /**
     * 根据service code获取service definition
     */
    public SyServiceDefinition getDefinitionByCode(String code) {
        return byCodeMap.get(code);
    }

    /**
     * 根据service class 获取service definition
     */
    public SyServiceDefinition getDefinitionByCode(Class<? extends SyService> serviceCls) {
        return byClassMap.get(serviceCls);
    }
}
